'######################################################################################
'Author  :         Sumit Gupta 
'Company :				 Vikasumit
'Date    :         12th August 2005
'--------------------------------------------------------------------------------------
'Working / Note on File : 
'Class for Making a client of the clsServer 
'It works With the clsServer 
'It only provide connection and Stream for data flow
'######################################################################################

Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Public Class clsClient

#Region "Private Temparory Varibles for Properties"

  Private Const CON_CLASSVERSION As String = "1.0.20050812" 'Define the Class Version
  Private t_Port As Integer
  Private t_IPAddress As IPAddress

  Private myThread As Thread
  Private myclient As TcpClient

  Private recieveData As String

#End Region

#Region "Public Events"

  Public Event ClientSetupMessage(ByVal msg As String, ByVal msgNo As Integer)
  Public Event ClientClosingError(ByVal msg As String, ByVal msgNo As Integer)
  Public Event ClientSetupError(ByVal msg As String, ByVal msgNo As Integer)
  Public Event DataRecieve(ByVal Data As String)
  Public Event ClientClosed()

#End Region

#Region "Public Property"

  'Get the Class Version
  Public ReadOnly Property ClassVersion() As String
    Get
      Return CON_CLASSVERSION
    End Get
  End Property

  Public Property IPAddr() As IPAddress
    Get
      Return t_IPAddress
    End Get
    Set(ByVal Value As IPAddress)
      t_IPAddress = Value
    End Set
  End Property

  Public Property Port() As Integer
    Get
      Return t_Port
    End Get
    Set(ByVal Value As Integer)
      t_Port = Value
    End Set
  End Property

#End Region

#Region "Private Function"

  'function to valid basic objects of the class
  Private Function ValidateData() As Boolean
    'If Me.Port < 1000 Then
    '  RaiseEvent ClientSetupError("Port No must be Greater than 1000", 10001)
    '  Return False
    'End If
    'If IPAddress.IsLoopback(Me.IPAddr) Then
    '  RaiseEvent ClientSetupError("You have provided the loop Back Ip Address", 10002)
    '  Return False
    'End If
    Return True
  End Function

  'Fucntion for Listening the connection string
  Private Sub Listener()

    ' Buffer for reading data
    Dim bytes(1024) As [Byte]
    Dim data As [String] = Nothing
    Dim i As Int32

        While True
            Try
                Dim nstream As NetworkStream
                Try
                    nstream = myclient.GetStream
                Catch ex As Exception
                    MsgBox(ex.Message)
                    Exit Sub
                End Try
                ' Loop to receive all the data sent by the client.
                i = nstream.Read(bytes, 0, bytes.Length)
                While (i <> 0)
                    ' Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                    recieveData = data
                    RaiseEvent DataRecieve(data)

                    ' Process the data sent by the client.
                    data = data.ToUpper()
                    nstream.Flush()
                    i = nstream.Read(bytes, 0, bytes.Length)

                End While
            Catch ex As Exception

            End Try
            
        End While
  End Sub

#End Region

#Region "Public Function"

  'Main sub call this for startign server
  Public Function StartClient() As Boolean
    If ValidateData() Then
      Try
        myclient = New TcpClient
        'MsgBox(Me.IPAddr.ToString & Me.Port)

        myclient.Connect(Me.IPAddr.ToString, Me.Port)
      Catch ex As SocketException
        RaiseEvent ClientSetupError("Socket Error :" & vbCrLf & ex.Message, 10005)
        Return False
      Catch ex As Exception
        'MsgBox(ex.Message)
        RaiseEvent ClientSetupError(ex.Message, 10005)
        Return False
      End Try
      'Set flags
      Try
        myThread = New Thread(AddressOf Listener)
        myThread.Start()
        Return True
      Catch ex As Exception

        RaiseEvent ClientSetupError(ex.Message, 10005)
        Return False
      End Try
    Else
        Return False
    End If
  End Function

  'Public Function to Make available the Values from the Network to Application
  Public Function GetInput() As String
    Return recieveData
  End Function

  'Public function to send the data to the network
  Public Function SendData(ByVal data As String) As Boolean
    Dim w As New System.IO.StreamWriter(myclient.GetStream)
    w.Write(data)
    w.Flush()
  End Function

  Public Sub CloseConnection()
    Try
      myclient.Close()
      myThread.Abort()
      RaiseEvent ClientClosed()
    Catch ex As Exception
      RaiseEvent ClientClosingError(ex.Message, 10004)
    End Try
  End Sub

#End Region

End Class